package com.huanghuai.retail.validator;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.huanghuai.retail.entity.PInformation;
import com.huanghuai.retail.entity.SInventory;
import com.huanghuai.retail.entity.Storage;
import com.huanghuai.retail.service.InformationService;
import com.huanghuai.retail.service.SInventoryService;
import com.huanghuai.retail.service.StorageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;

@Component
public class InventoryValidator implements Validator {

    @Resource
    private InformationService sInformationService;

    @Resource
    private SInventoryService sInventoryService;

    @Autowired
    private StorageService sStorageService;


    private static InformationService informationService;
    private static SInventoryService inventoryService;

    private static  StorageService storageService;

    @PostConstruct
    private void init() {
        informationService = sInformationService;
        inventoryService = sInventoryService;
        storageService = sStorageService;
    }


    @Override
    public boolean supports(Class<?> clazz) {
        return SInventory.class.equals(clazz);
    }

    @Override
    public void validate(Object target, Errors errors) {
        SInventory sInventory = (SInventory) target;
        Storage storage = storageService.getById(sInventory.getStorageId());
        PInformation information =
                informationService.getById(sInventory.getProductId());
        if (information == null) {
            errors.rejectValue("productId", "500",
                    "当前产品不存在");
        } else if (storage == null) {
            errors.rejectValue("storageId", "500",
                    "当前仓库不存在");
        } else if (sInventory.getNumber() == null ||
                sInventory.getNumber() < 0) {
            errors.rejectValue("number",
                    "500",
                    "库存量不能为空，且必须大于等于0");
        } else if (sInventory.getLastNumber() == null ||
                sInventory.getLastNumber() < 0) {
            errors.rejectValue("lastNumber", "500",
                    "上一次库存量不能为空，且必须大于等于0");
        } else if (sInventory.getMinRate() == null ||
                sInventory.getMinRate() <= 0 ||
                sInventory.getMinRate() >= 50) {
            errors.rejectValue("minRate", "500",
                    "最低库存比不能为空，且范围在 1 到 49 之间");
        } else {
            LambdaQueryWrapper<SInventory> sInventoryLambdaQueryWrapper
                    = new LambdaQueryWrapper<>();
            sInventoryLambdaQueryWrapper.eq(SInventory::getProductId,
                            sInventory.getProductId())
                    .eq(SInventory::getStorageId,
                            sInventory.getStorageId());
            List<SInventory> inventories =
                    inventoryService.list(sInventoryLambdaQueryWrapper);
            if (inventories.size() > 0) {
                errors.rejectValue("productId", "501",
                        "该产品在仓库中已存在");
            }
        }
    }
}
